//
//  bossCellTwoRow.swift
//  LeDian
//
//  Created by Zhongnan on 16/3/3.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit


class SearchViewsCell: UITableViewCell {
    
    let SameBtnTag = 555
    var tapClosure:((selCell:SearchViewsCell,index:Int)->Void)?
    //标题
    var title = String() {
        
        didSet{
            
            titleLab?.text = title
        }
    }
    //标签
    var titleTagsArr = [String]() {
        
        didSet{
            
            NSOperationQueue.mainQueue().addOperationWithBlock { [weak self] in
                
                self?.tagView?.titleParams = [String]()
                self?.tagView?.titleParams = self!.titleTagsArr
                let height = self?.tagView.totalLabHeight
                
                self?.tagView.snp_updateConstraints { (make) in
                    make.top.equalTo(self!.titleLab.snp_bottom)
                    make.left.right.equalTo(0)
                    make.height.equalTo(height!)
                }
            }
        }
    }
    
    var models:SearchViewsCellModels?
    var tagView:LDBaseManyTagsView!
    var titleLab:UILabel!
    
    var cellHeight:CGFloat?
    
    //MARK: - init初始化
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    
        setDefaultContent()
        setupFrame()
        
    }
    
    
    
    /**
     加入到主视图中
     */
    func setDefaultContent(){
        
        selectionStyle = UITableViewCellSelectionStyle.None
        backgroundColor = LDGolbalColor.White
        
        tagView = LDBaseManyTagsView()
        tagView.sendTapTag { [weak self](index) in
            if self?.tapClosure != nil {
                self?.tapClosure!(selCell: self!,index: index)
            }
        }
        titleLab = UILabel.setDetailsLab("设计", fontSize: 15, titleColor: LDFontColor.DeepGray, Alignment: .Left)
        
        addSubview(tagView)
        addSubview(titleLab)
        
        titleLab.snp_makeConstraints { (make) in
            make.top.equalTo(LDMargin_V*4)
            make.left.equalTo(LDMargin_V*5)
            make.height.equalTo(25)
        }
        
        tagView.snp_makeConstraints { (make) in
            make.top.equalTo(titleLab.snp_bottom)
            make.left.right.equalTo(0)
        }
        
        let divline = UIView()
        divline.backgroundColor = LDIconColor.LightGray
        addSubview(divline)
        divline.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSizeMake(LDFrame.ScreenWidth, 0.5))
            make.left.bottom.equalTo(0)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    func sendLabClosure(closure:(selCell:SearchViewsCell,index:Int)->Void) {
        tapClosure = closure
    }
    
    //MARK: - 模型回来后的frame
    /**
     自动布局
     */
    func setupFrame(){
        
    }
}

extension SearchViewsCell {
    
    class func getCellHeight(titleParams:Array<String>) ->CGFloat{
        
        var totalLabHeight:CGFloat = 0
//        frame = LDFrame.ScreenBounds
        var newtitleParams:Array<String> = titleParams
        
        let count:Int = newtitleParams.count
        
        let labX_Origin = Margin_V*5
        var labX:CGFloat = Margin_V*5
        var labY:CGFloat = Margin_V*3
        var totalW:CGFloat = Margin_V*4
        
        let labH:CGFloat = 30
        
        for i in 0..<count {
            
            
            let rect = NSString.init(string: newtitleParams[i]).boundingRectWithSize(CGSizeMake(300, 13), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font13], context: nil)
            
            let labW = rect.width + LDMargin_H*2
            totalW += labW + LDMargin_H*2
            
            if totalW > (LDFrame.ScreenWidth) {
                totalW = Margin_H*4 + labW
                labX = labX_Origin
                labY = labY + labH + LDMargin_V*3
            }
            
            labX = labX + labW + LDMargin_H*2
            
            totalLabHeight =  labY + labH + Margin_V*4
            
        }
        return totalLabHeight + LDMargin_V*4 + 25//头部
    }
}

class SearchViewsCellModels: NSObject {
    
    let margin = Margin_V * 3
    let btnH:CGFloat = 40
    
    var category:NSMutableArray?
    var teamtitle:String = ""
    var cellheight:CGFloat?
}
